
//
//  JGCountDownButton.m
//  JHWAccompany
//
//  Created by Magician on 2018/4/3.
//  Copyright © 2018年 Jugame Inc. All rights reserved.
//

#import "JGCountDownButton.h"

@interface JGCountDownButton ()

@property (nonatomic, strong) NSString *countTitle;
@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation JGCountDownButton

+ (instancetype)countDownButtonWithTitle:(NSString *)title
                          countDownTitle:(NSString *)countTitle
                              titleColor:(UIColor *)color
                     countDownTitleColor:(UIColor *)countColor
{
    JGCountDownButton *countBtn = [JGCountDownButton buttonWithType:UIButtonTypeCustom];
    
    [countBtn setTitleColor:color forState:UIControlStateNormal];
    [countBtn setTitleColor:countColor forState:UIControlStateDisabled];
    [countBtn setTitle:title forState:UIControlStateNormal];
    [countBtn setTitle:countTitle forState:UIControlStateDisabled];
    countBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    countBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 16);
    countBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    countBtn.countTitle = countTitle;
    
    return countBtn;
}

+ (JGCountDownButton *)startWithTitle:(NSString *)title
                       countDownTitle:(NSString *)subTitle
                            mainColor:(UIColor *)mColor
                           countColor:(UIColor *)color
{
    JGCountDownButton *button = [[JGCountDownButton alloc] init];
    
    //倒计时时间
    __block NSInteger timeOut;
    if (button.time > 0) {
        timeOut = button.time;
    }else {
        timeOut = 0;
    }
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = mColor;
                [button setTitle:title forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGB(0xd5d5d5) forState:UIControlStateNormal];
                button.userInteractionEnabled = YES;
            });
        } else {
            int seconds = timeOut % 60;
            NSString *timeStr = [NSString stringWithFormat:@"%d", seconds];
            if (seconds == 0) {
                timeStr = [NSString stringWithFormat:@"%ld",(long)timeOut];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = color;
                [button setTitle:[NSString stringWithFormat:@"%@s",timeStr] forState:UIControlStateNormal];
                button.userInteractionEnabled = NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
    
    return button;
    
    
}

- (void)resetWithTitle:(NSString *)title
{
    _timer ? dispatch_source_cancel(_timer) : nil;
    self.enabled = YES;
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)startDown{
    //倒计时时间
    __block NSInteger timeOut = self.time;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.enabled = YES;
            });
        } else {
            NSInteger seconds = timeOut % self.time;
            NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)seconds];
            if (seconds == 0) {
                timeStr = [NSString stringWithFormat:@"%ld",(long)timeOut];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:[NSString stringWithFormat:@"%@%@",timeStr,self.countTitle]
                      forState:UIControlStateDisabled];
                self.enabled = NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}
@end
